<template>
  <PublicPage pageName="扫码登录" borderType pageBgcolor="#ffffff" class="seller-login">
    <view class="seller-pc">
      <image src="https://qqposs.oss-cn-shenzhen.aliyuncs.com/mpAppImg/bgImg/seller-pc.png" class="pc-image"/>
      <view class="pc-txt">速速团PC端管理后台登录确认</view>
    </view>
    <view class="group-button">
      <button class="confirm" hover-class="none" :disabled="disabled" @tap="getUserProfile">确认登录</button>
      <button class="cancel" hover-class="none" @click="cancelLogin">取消登录</button>
    </view>
  </PublicPage>
</template>

<script>
import PublicPage from '@/components/publicPage/index.vue'
import userApi from '@/api/user.js'

export default {
  name: 'sellerLogin',
  components: {
    PublicPage
  },
  onLoad(option) {
    if (option.scene) {
      this.scene = option.scene
    }
  },
  data() {
    return {
      disabled: false,
      scene: ''
    }
  },
  methods: {
    confirmLogin() {
      this.disabled = true
      userApi.sellerLogin({pollingKey: this.scene}).then(res => {
        if (res.code === 200) {
          uni.showToast({
            title: 'PC管理后台登录成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(() => {
            this.cancelLogin()
          }, 1000)
        }
      }).catch(() => {
        this.disabled = false
      })
    },
    getUserProfile() {
      if (this.isLogin) {
        this.confirmLogin()
      } else {
        const accountInfo = wx.getAccountInfoSync();
        wx.getUserProfile({
          desc: '用于完善用户信息',
          success: (result) => {
            wx.login({
              success: (res) => {
                const params = {
                  ...result.userInfo,
                  nickname: result.userInfo.nickName,
                  auth_appid: accountInfo.miniProgram.appId,
                  code: res.code
                }
                if (res.code) {
                  if (this.$store.state.loginShareIdCode) params.recommendCode = this.$store.state.loginShareIdCode // 绑定推荐人
                  userApi.authLogin(params).then(_res => {
                    if (_res.code === 200) {
                      uni.setStorageSync('token', _res.data.token);
                      this.$store.commit('TOKEN', _res.data.token);
                      this.$store.commit('USERINFO', _res.data.userInfoVO);
                      this.setIsRefreshType(true)
                      this.confirmLogin()
                    }
                  })
                }
              }
            })
          }
        })
      }
    },
    cancelLogin() {
      uni.switchTab({url: '/pages/index/index'})
    },
  }
}
</script>

<style lang="scss" scoped>
.seller-login {
  position: relative;

  .seller-pc {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 240rpx;
  }

  .pc-image {
    width: 320rpx;
    height: 256rpx;
  }

  .pc-txt {
    width: 300rpx;
    margin-top: 32rpx;
    font-size: 32rpx;
    line-height: 56rpx;
    text-align: center;
    color: $n8Color;
  }

  .group-button {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    padding-bottom: 240rpx;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
  }

  .confirm {
    width: 400rpx;
    height: 88rpx;
    line-height: 88rpx;
    border-radius: 64rpx;
    font-size: 32rpx;
    background-color: $themeColor;
    color: #FFFFFF;
  }

  .cancel {
    margin-top: 36rpx;
    font-size: 28rpx;
    color: $n6Color;
  }
}
</style>